<!-- 手动添加黑名单 -->
<template>
  <el-dialog
    class="black-dialog"
    title="手动添加"
    :visible.sync="dialogVisible"
    width="680px"
    v-loading="dialogLoading"
    :before-close="handleClose"
    v-dialogDrag>
    <div style="padding:0px 20px">
      <el-form ref="form" :model="formData" :inline="true" class="ky-form-inline ky-form-inline-sm dialog-form" label-width="83px">
        <!--  <el-form-item label="医院名称:" prop="hospitalCode">
          <el-select size="small" class="param-type_input" v-model="formData.hospitalCode" placeholder="请选择" @change="hospitalChange">
            <el-option
              v-for="(item,index) in mx_hospitalList"
              :key="index"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-tooltip class="item" effect="light" content="精确查询" placement="top-start">
          <el-form-item label="患者姓名:" prop="patientName" class="auto-width">
            <el-input prefix-icon="el-icon-search" v-model="formData.patientName" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-tooltip>
        <el-tooltip class="item" effect="light" content="精确查询" placement="top-start">
          <el-form-item label="患者ID号:" prop="patientId" class="auto-width">
            <el-input prefix-icon="el-icon-search" v-model="formData.patientId" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-tooltip>
        <el-form-item>
          <el-button class="m-l-0" size="small" @click="resetParam" plain>重置</el-button>
          <el-button type="primary" class="m-l-0" size="small" icon="el-icon-search" @click="search">查询</el-button>
        </el-form-item>
        <!-- <el-form-item label="手机号:" prop="hospitalCode">
          <el-input prefix-icon="el-icon-search" v-model="formData.patientName" placeholder="请输入内容"></el-input>
        </el-form-item> -->
      </el-form>
      <el-table
        ref="table"
        v-loading="loading"
        size="medium"
        :data="tableData"
        style="width: 100%"
        class="ky-table"
        max-height="350px"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <!-- <el-table-column align="left" prop="hospitalName" label="医院名称"></el-table-column> -->
        <el-table-column align="left" prop="patientName" label="患者姓名"></el-table-column>
        <el-table-column align="left" prop="patientId" label="患者ID号"></el-table-column>
        <!--  <el-table-column align="left" prop="patientName" label="手机号"></el-table-column>
        <el-table-column align="left" prop="dep" label="预约部门"></el-table-column>
        <el-table-column align="left" prop="modality" label="预约机房"></el-table-column> -->
      </el-table>
    </div>
    <el-form ref="ruleform" :model="ruleForm" :rules="rules" :inline="true" class="ky-form-inline ky-form-inline-sm main-form">
      <el-form-item label="黑名单时间：" prop="autoBanDay" class="short-input">
        <el-select size="small" v-model="ruleForm.autoBanDay" placeholder="请选择天数" clearable>
          <el-option
            v-for="(item,index) in timesOptions"
            :key="index"
            :label="item.item"
            :value="Number(item.itemValue)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="黑名单申请限制：" prop="type" class="short-input">
        <!-- <el-checkbox-group v-model="ruleForm.type">
          <el-checkbox label="转诊预约" name="type">转诊预约</el-checkbox>
          <el-checkbox label="外院预约" name="type">外院预约</el-checkbox>
          <el-checkbox label="临床预约" name="type">临床预约</el-checkbox>
          <el-checkbox label="自助预约" name="type">自助预约</el-checkbox>
        </el-checkbox-group> -->
        <!-- <el-radio-group v-model="ruleForm.type">
          <el-radio label="转诊预约"></el-radio>
          <el-radio label="外院预约"></el-radio>
          <el-radio label="临床预约"></el-radio>
          <el-radio label="自助预约"></el-radio>
        </el-radio-group> -->
        <el-select size="small" multiple collapse-tags v-model="ruleForm.type" placeholder="请选择来源" clearable>
          <el-option
            v-for="(item,index) in typeOptions"
            :key="index"
            :label="item.item"
            :value="item.item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="封禁说明：" prop="banDesc" class="short-input" style="margin-right: 0px!important;width: 630px;">
        <el-input maxlength="255" show-word-limit v-model="ruleForm.banDesc" placeholder="请输入内容"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button size="small" type="primary" @click="save">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// import paginationCont from '@/components/Pagination/index'
import { getItemList, addToBlackLis, getPeoplePatlist } from '@/api/api.js'
export default {
  name: '',
  mixins: [],
  components: { },
  props: {
    addVisible: {
      type: Boolean,
      default: false
    },
    hospitalCode: {
      String,
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: false,
      formData: {
        hospitalCode: '',
        patientName: '',
        patientId: ''
      },
      ruleForm: {
        autoBanDay: null,
        type: [],
        banDesc: ''
      },
      typeOptions: [],
      loading: false,
      dialogLoading: false,
      tableData: [],
      timesOptions: [],
      multipleSelection: [],
      rules: {
        autoBanDay: { required: true, message: '黑名单时间必填', trigger: ['blur', 'change'] },
        type: { required: true, message: '黑名单申请限制必填', trigger: ['blur', 'change'] },
        banDesc: { required: true, message: '封禁说明必填', trigger: ['blur', 'change'] }
      }
    }
  },
  watch: {
    addVisible(val) {
      this.dialogVisible = val
    },
    hospitalCode: {
      handler(val) {
        this.formData.hospitalCode = val
        this.hospitalChange()
      },
      immediate: true
    }
  },
  computed: {},
  created() {

  },
  mounted() {
    // this.$getHospitalList()
  },
  methods: {
    resetParam() {
      // 重置
      this.$refs.form.resetFields()
    //   this.search()
    },
    search() {
      // 查询
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.currentPage = 1
          this.fetchData()
        }
      })
    },
    handleClose() {
      this.$refs.table.clearSelection()
      this.multipleSelection = []
      this.$refs.ruleform.resetFields()
      this.tableData = []
      this.resetParam()
      this.$emit('update:addVisible', false)
    },
    save() {
      this.$refs.ruleform.validate(valid => {
        if (valid) {
          if (!this.multipleSelection || this.multipleSelection.length === 0) {
            this.$message.error('请至少选择一位患者')
            return
          }
          let arr = []
          for (let item of this.multipleSelection) {
            for (let v of this.ruleForm.type) {
              arr.push({ patName: item.patientName, patientId: item.patientId, appointmentType: v })
            }
          }
          let data = {
            hospitalCode: this.formData.hospitalCode,
            banDesc: this.ruleForm.banDesc,
            autoBanDay: this.ruleForm.autoBanDay,
            patAndAppointmentTypes: arr
          }
          this.dialogLoading = true
          addToBlackLis(data).then(res => {
            if (res && res.code === 10000) {
              this.$message.success('添加成功')
              this.$emit('refresh')
              this.handleClose()
            }
            this.dialogLoading = false
          }).catch(() => {
            this.dialogLoading = false
          })
        } else {
          return false
        }
      })
    },
    hospitalChange() {
      this.getTimesOptions()
      this.getTypeOptions()
    },
    changePagenation() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.fetchData()
        }
      })
    },
    fetchData() {
      if (!this.formData.patientId && !this.formData.patientName) {
        this.$message.warning('患者姓名和患者ID号必填一个')
        return
      }
      this.loading = true
      getPeoplePatlist(this.formData).then(res => {
        this.loading = false
        if (res && res.code === 10000) {
          this.tableData = res.content.list
        } else {
          this.tableData = []
        }
      }).catch(() => {
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getTimesOptions() {
      // 获取预约来源
      let data = {
        HospitalCode: this.formData.hospitalCode,
        type: '解禁时间'
      }
      getItemList(data).then(res => {
        if (res && res.code === 10000) {
          this.timesOptions = res.content.list.sort((a, b) => {
            return a.itemValue - b.itemValue
          })
        }
      })
    },
    getTypeOptions() {
      let data = {
        HospitalCode: this.formData.hospitalCode,
        type: '预约来源'
      }
      getItemList(data).then(res => {
        if (res && res.code === 10000) {
          this.typeOptions = res.content.list
        }
      })
    }
  },
  beforeDestroy() {},
  destroyed() {},
  beforeUpdate() {},
  updated() {}
}
</script>
<style lang='scss' scoped>
.black-dialog{
    /deep/ .el-dialog__body{
        padding: 10px 0px;
    }
}
.dialog-form{
    margin-bottom: 10px;
    .auto-width{
        width: 36%;
        /deep/ .el-form-item__content{
            width: calc(100% - 83px);
        }
    }
}
.short-input{
    width: calc(70% - 232px);
    margin-right: 23px!important;
    margin-bottom: 5px;
    /* .el-input{
        width: 200px;
    } */
    .word{
        margin-left: 5px;
    }
    /deep/ .el-form-item__content{
        width: 100%;
        .el-select{
            width: 100%;
            .el-input{
                width: 100%;
            }
        }
    }
}
.long-input{
    width: 410px;
    margin-right: 23px!important;
    margin-bottom: 5px;
    /deep/ .el-form-item__content{
        margin-top: 10px;
    }
}
.main-form{
    padding: 15px 20px;
    background: #F3F6FB;
}
.ky-table{
    .el-table__body-wrapper{
        max-height: 300px;
        overflow: auto;
    }
}
</style>
